﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using fr = HK.FiscalRegistrar.FiscalRegistrar; 
using h = functionService.shorts;
using da = functionService.DATECOD;
using vv = functionService.serfun_city7;
using System.Collections;
namespace ETALON
{
   
    namespace ZAKAZ
    {
        enum modeWAED
        {
            work,
            addZakaz, addPlase, addProd,
            selectZakaz, selectPlase, selectProd,
            deleteZakaz, deletePlase, deleteOrEdit_Prod,
            deleteOrEdit_ProdAfter, 
            editProd,
            editProdAfter,
            selectZakazFiskal,
            selectZakazPredchek,
            selectPlacePredchek,
            addProdPredchek
        }
    }
    namespace REPORTS
    {
        enum reports
        {
            check, prihod, rashod
        }
        enum reportFields_check { name, kol, pr, sd }
    }
    public partial class FormZl : Form
    {

        DataTable dz, dzp, dzt, dztp, dzt_itogo; functionService.prm_city q; ZAKAZ.modeWAED cuurentModeWAED;
        REPORTS.PRINT prnt; 
        
        public FormZl()
        {
            InitializeComponent(); InitializeComponentMy();
        }

        #region ini
        private void InitializeComponentMy()
        {
            dz = new DataTable();
            q = new functionService.prm_city();
            q.con.pass =  q.tk(param.pass_inServ_55); q.con.co(q.tk(param.server_1));
            //vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz = q.tk_int(param.show_fpa_0InWork_1fiskal_888all_39);
            q.put((int)param.select_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz_47, 20);
            vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz = 20;

            load(); cuurentModeWAED = ETALON.ZAKAZ.modeWAED.work; 
            q.put(param.tree_MODE_WAED_38, (int)cuurentModeWAED);
            prnt = new ETALON.REPORTS.PRINT();
            lableSet(vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz);
            vv.fiskalDocAfterPrint_0no_1yes = q.tk_int (param.fiskalDocAfterPrint_0no_1yes_40);
           // h.m(h.az(q.tk_int(param.fiskalDocAfterPrint_0no_1yes_40), vv.fiskalDocAfterPrint_0no_1yes));

            int ics = vv.o_l_0_cod ;//q.tc_int(param.o_l_0);
            if (ics == 4 || ics == 19)
            {
                toolStripMenuItem4.Visible = true;
                toolStripMenuItem3.Visible = true;
                toolStripMenuItem5.Visible = true;
                toolStripMenuItem6.Visible = true;
            }
            else
            {
                toolStripMenuItem4.Visible = false;
                toolStripMenuItem3.Visible = false;
                toolStripMenuItem5.Visible = false;
                toolStripMenuItem6.Visible = false;

            }
            

            if (vv.fiskalDocAfterPrint_0no_1yes == 1) toolStripMenuItem5.Checked = true  ;   else   toolStripMenuItem5.Checked = false ;

        }

        private void load()
        {
            listView3.Items.Clear(); string sddd = q.tk(param.day_work_7);
            dz = q.selectAny(string.Concat("call `city`.`zakaz_rd_load_sh012` (",
                da.codDATE_10913_from_DATETIME(sddd), ",", 
                vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz ,");"));
            ListViewItem lvi; int i0=0, i1=1, i2 = 2;

            //q.sh(dz);
            foreach(DataRow r in dz.Rows)
            {
                lvi = new ListViewItem(); 
                lvi.Text = functionService.shorts.a("Заказ_", r[0].ToString().Substring(5, 3)); 
                lvi.Tag = r[0].ToString();

                if (r[1].ToString() == i0.ToString())
                {  lvi.ImageIndex = 13; }
                else if (r[1].ToString() == i1.ToString())
                {  lvi.ImageIndex = 15; lvi.ForeColor = Color.LightGray; }
                else if (r[1].ToString() == i2.ToString())
                {  lvi.ImageIndex = 3; }
                
                listView3.Items.Add(lvi);
            }

            buttonPlaseAddno();
            string sql_dml = string.Concat( "SELECT pr2 FROM listtov l where cod=", q.tk(param.zakaz_defaultValue_33) ,";");
            string strPriceDefaultValueCT = q.selectScalar(sql_dml).ToString(); 
            q.put(param.zakaz_defaultValue_price_34, strPriceDefaultValueCT);
            calkCassa();

            //q.put();
        }
        #endregion

        

        #region events
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            int icd = q.tk_int_codDate_fromDate(param.day_work_7);
            int i1 = da.codDATE_10913_from_codDATE_20100913(icd);

            string sql = h.a("SELECT right(codd,3)*1 as codd FROM zakaz_rd z where left(codd,5) =", i1, " order by codd;");
            //h.m(sql);
            dzp = q.selectAny(sql);
            //int ic =  nextCod.codNext(dzp); //fs.shorts.m(ic);
            //q.sh(dzp);
            int ic = functionService.nextCod.codNext2(dzp);
            int icc = 0; //h.m(ic);
            if (ic == 0) { icc = i1 * 1000 + 1; } else { icc = i1 * 1000 + ic; }
            string sql_dml_add = sql_SAED.ADD_zakaz(icc, icd, 2); //h.m(sql_dml_add);
            q.UpdateData(sql_dml_add); load();
        }

        private void listView3_ItemActivate(object sender, EventArgs e)
        {
           
        }

        private void listView3_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            
        }

        private void listView3_ItemChecked(object sender, ItemCheckedEventArgs e)
        {

        }
               
        private void listView3_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
           
        }
        private void calkCassa()
        {
            string sss = MI_SUMMA_CASSA_L;
            q.put((int)param.kassa_sum_smena_46, sss);
            this.Text = string.Concat("Сумма по кассе :", sss);
            q.UpdateData("DELETE FROM city.zakaz_td where ct=95;");
        }
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            FormTreeSelect f = new FormTreeSelect("city", "listtable", loadPlasesIvLisstView, false); f.ShowDialog();
            calkCassa();
        }
        private void loadPlasesIvLisstView()
        {
            functionService.prm pe = new functionService.prm("PLP_treeSelect");
            string scod = functionService.nameCod.readCod(pe.tk(1)); int icod0000 = Convert.ToInt32(scod); //1- what select
           // int icod0000 = q.tc_int(param.treeView_select_14);
                int icod = icod0000 - 10000;// h.m(icod0000);
                int ict = q.tk_int(param.zakaz_defaultValue_33);
            string sql_dml_add = sql_SAED.ADD_plase(q.tk_int(param.zakaz_current_13), icod, ict, q.tk_double(param.zakaz_defaultValue_price_34));
           // h.m(sql_dml_add);
            q.UpdateData(sql_dml_add); 
            loadPlases();
        }
        //private void loadPlasesIvLisstView(int icod0000o)
        //{
        //    //int icod0000 = q.tc_int(param.treeView_select_14), icod = icod0000 - 10000; h.m(icod0000);
        //    int icod = icod0000o - 10000; h.m(icod0000o);
        //    string sql_dml_add = sql_SAED.ADD_plase(q.tk_int(param.zakaz_current_13), icod);
        //    h.m(sql_dml_add);
        //    q.UpdateData(sql_dml_add);
        //    loadPlases();
        //}
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            q.put(param.tree_position_Width_35 , q.tk(param.tree_position_Width_TOV_37));
            if (vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz == 2)
            { 

            }
            else if (vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz == 1)
            {
 
            }
            setModeWAED(ETALON.ZAKAZ.modeWAED.addProd );
           // FormTreeSelect f = new FormTreeSelect("city", "listtov", Add_ProdKol); f.Show();
            enterData.FormED2 f = new enterData.FormED2(); f.ShowDialog();
            refrashlistView2Prod();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {


            //try
            //{

            //    string s = listView1.SelectedItems[0].Text;
            //    q.put(param.zakaz_plase_current_15, listView1.SelectedItems[0].Tag);
            //    setModeWAED(ETALON.ZAKAZ.modeWAED.selectPlase);
            //   // buttonProdAdd(s);
            //}
            //catch { buttonPlaseAddno(); buttonProdAddno(); }
            
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            try
            {

                string s = listView1.SelectedItems[0].Text; toolStripLabel2.Text = s;
                q.put(param.zakaz_plase_current_15, listView1.SelectedItems[0].Tag);
                
                if (vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz  == 1)
                { 
                    setModeWAED(ETALON.ZAKAZ.modeWAED.selectZakazFiskal);
                }
                else if (vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz == 2)
                {
                    setModeWAED(ETALON.ZAKAZ.modeWAED.selectPlacePredchek);
                }
                else 
                { 
                    setModeWAED(ETALON.ZAKAZ.modeWAED.selectPlase);
                }
                refrashlistView2Prod();
            }
            catch 
            { //buttonPlaseAddno(); buttonProdAddno();
            }
        }

        #endregion



        #region work function

        #region MODE
        private void setModeWAED(ZAKAZ.modeWAED mo)
        {
            q.put(param.tree_MODE_WAED_38, (int)mo);

            if (mo == ETALON.ZAKAZ.modeWAED.work)
            {

            }
            else if (mo == ETALON.ZAKAZ.modeWAED.addZakaz)
            {
                #region Enabled true false
                toolStripButton3.Enabled = true;
                toolStripButton4.Enabled = true;

                listView3.Enabled = true;

                toolStripMenuItem1.Enabled = true;
                toolStripMenuItem2.Enabled = true;
                toolStripMenuItem7.Enabled = false; //сервис

                toolStripButton5.Enabled = false;
                toolStripButton7.Enabled = false;
                toolStripButton1.Enabled = false;
                toolStripButton2.Enabled = false;
                toolStripButton6.Enabled = false;


                toolStripLabel1.Enabled = false;
                toolStripLabel2.Enabled = false;

                listView1.Enabled = false;
                listView2.Enabled = false;
                listView4.Enabled = false;
                #endregion
            }
            else if (mo == ETALON.ZAKAZ.modeWAED.selectZakazFiskal)
            {
                #region Enabled true false
                toolStripButton3.Enabled = false;
                toolStripButton4.Enabled = false;

                listView3.Enabled = true ;

                toolStripMenuItem1.Enabled = false;
                toolStripMenuItem2.Enabled = false;
                toolStripMenuItem7.Enabled = false; //сервис

                toolStripButton5.Enabled = false;
                toolStripButton7.Enabled = false;
                toolStripButton1.Enabled = false;
                toolStripButton2.Enabled = false;
                toolStripButton6.Enabled = false;


                toolStripLabel1.Enabled = false;
                toolStripLabel2.Enabled = false;

                listView1.Enabled = true;
                listView2.Enabled = false;
                listView4.Enabled = false;
                toolStripMenuItem7.Enabled = false;
                #endregion
            }
            else if (mo == ETALON.ZAKAZ.modeWAED.addPlase)
            {

            }
            else if (mo == ETALON.ZAKAZ.modeWAED.addProd)
            {
                #region Enabled true false
                toolStripButton3.Enabled = false;//доб зак
                toolStripButton4.Enabled = false;//удал заказ

                listView3.Enabled = true;

                toolStripMenuItem1.Enabled = true; //чек
                toolStripMenuItem2.Enabled = true; //сервис
                toolStripMenuItem7.Enabled = false; //сервис
               
                toolStripButton5.Enabled = true;//доб пм
                toolStripButton7.Enabled = false;//удал пм
                toolStripButton1.Enabled = true;//доб прод
                toolStripButton2.Enabled = true;//удал прод
                toolStripButton6.Enabled = true;//ред прод

                toolStripLabel1.Enabled = false;//зак 
                toolStripLabel2.Enabled = false;//пм

                listView1.Enabled = true;//пм
                listView2.Enabled = true;//прод
                listView4.Enabled = true;//прод обобщ
                #endregion
            }
            else if (mo == ETALON.ZAKAZ.modeWAED.selectZakaz)
            {
                #region Enabled true false
                toolStripButton3.Enabled = true;//доб зак
                toolStripButton4.Enabled = true;//удал заказ

                listView3.Enabled = true;

                toolStripMenuItem1.Enabled = true; //чек
                toolStripMenuItem2.Enabled = true; //сервис
                toolStripMenuItem7.Enabled = true;

                toolStripButton5.Enabled = true;//доб пм
                toolStripButton7.Enabled = false;//удал пм
                toolStripButton1.Enabled = false;//доб прод
                toolStripButton2.Enabled = false;//удал прод
                toolStripButton6.Enabled = false;//ред прод

                toolStripLabel1.Enabled = false;//зак 
                toolStripLabel2.Enabled = false;//пм

                listView1.Enabled = true;//пм
                listView2.Enabled = false;//прод
                listView4.Enabled = false;//прод обобщ
                #endregion
            }
            else if (mo == ETALON.ZAKAZ.modeWAED.selectPlase)
            {
                #region Enabled true false
                toolStripButton3.Enabled = true;//доб зак
                toolStripButton4.Enabled = false;//удал заказ

                listView3.Enabled = true;

                toolStripMenuItem1.Enabled = true; //чек
                toolStripMenuItem2.Enabled = true; //сервис
                toolStripMenuItem7.Enabled = false;

                toolStripButton5.Enabled = true;//доб пм
                toolStripButton7.Enabled = true;//удал пм
                toolStripButton1.Enabled = true;//доб прод
                toolStripButton2.Enabled = false;//удал прод
                toolStripButton6.Enabled = false;//ред прод

                toolStripLabel1.Enabled = false;//зак 
                toolStripLabel2.Enabled = false;//пм

                listView1.Enabled = true;//пм
                listView2.Enabled = true;//прод
                listView4.Enabled = true;//прод обобщ


                #endregion
            }
            else if (mo == ETALON.ZAKAZ.modeWAED.selectPlacePredchek || mo == ETALON.ZAKAZ.modeWAED.addProdPredchek)
            {
                #region Enabled true false
                toolStripButton3.Enabled = true;//доб зак
                toolStripButton4.Enabled = false;//удал заказ

                listView3.Enabled = true;

                toolStripMenuItem1.Enabled = true; //чек
                toolStripMenuItem2.Enabled = true; //сервис
                toolStripMenuItem7.Enabled = false;

                toolStripButton5.Enabled = true;//доб пм
                toolStripButton7.Enabled = false;//удал пм
                toolStripButton1.Enabled = true;//доб прод
                toolStripButton2.Enabled = false;//удал прод
                toolStripButton6.Enabled = false;//ред прод

                toolStripLabel1.Enabled = false;//зак 
                toolStripLabel2.Enabled = false;//пм

                listView1.Enabled = true;//пм
                listView2.Enabled = true;//прод
                listView4.Enabled = true;//прод обобщ


                #endregion
            }
            else if (mo == ETALON.ZAKAZ.modeWAED.selectZakazPredchek )
            {
                #region Enabled true false
                toolStripButton3.Enabled = true;//доб зак
                toolStripButton4.Enabled = false;//удал заказ

                listView3.Enabled = true;

                toolStripMenuItem1.Enabled = true; //чек
                toolStripMenuItem2.Enabled = true; //сервис
                toolStripMenuItem7.Enabled = false;


                toolStripButton5.Enabled = true;//доб пм
                toolStripButton7.Enabled = false;//удал пм
                toolStripButton1.Enabled = true;//доб прод
                toolStripButton2.Enabled = false;//удал прод
                toolStripButton6.Enabled = false;//ред прод

                toolStripLabel1.Enabled = false;//зак 
                toolStripLabel2.Enabled = false;//пм

                listView1.Enabled = true;//пм
                listView2.Enabled = true;//прод
                listView4.Enabled = true;//прод обобщ


                #endregion
            }
            else if (mo == ETALON.ZAKAZ.modeWAED.selectProd)
            {

            }
            else if (mo == ETALON.ZAKAZ.modeWAED.deleteZakaz)
            {

            }
            else if (mo == ETALON.ZAKAZ.modeWAED.deletePlase)
            {

            }
            else if (mo == ETALON.ZAKAZ.modeWAED.deleteOrEdit_Prod)
            {
                #region Enabled true false
                toolStripButton3.Enabled = false;//доб зак
                toolStripButton4.Enabled = false;//удал заказ

                listView3.Enabled = true;

                toolStripMenuItem1.Enabled = true; //чек
                toolStripMenuItem2.Enabled = true; //сервис

                toolStripButton5.Enabled = false;//доб пм
                toolStripButton7.Enabled = false;//удал пм
                toolStripButton1.Enabled = true;//доб прод
                toolStripButton2.Enabled = true;//удал прод
                toolStripButton6.Enabled = true;//ред прод

                toolStripLabel1.Enabled = false;//зак 
                toolStripLabel2.Enabled = false;//пм

                listView1.Enabled = true;//пм
                listView2.Enabled = true;//прод
                listView4.Enabled = false;//прод обобщ


                #endregion
            }
            else if (mo == ETALON.ZAKAZ.modeWAED.deleteOrEdit_ProdAfter )
            {
                #region Enabled true false
                toolStripButton3.Enabled = false;//доб зак
                toolStripButton4.Enabled = false;//удал заказ

                listView3.Enabled = false;

                toolStripMenuItem1.Enabled = true; //чек
                toolStripMenuItem2.Enabled = true; //сервис

                toolStripButton5.Enabled = false;//доб пм
                toolStripButton7.Enabled = false;//удал пм
                toolStripButton1.Enabled = true;//доб прод
                toolStripButton2.Enabled = true;//удал прод
                toolStripButton6.Enabled = true;//ред прод

                toolStripLabel1.Enabled = false;//зак 
                toolStripLabel2.Enabled = false;//пм

                listView1.Enabled = true;//пм
                listView2.Enabled = true;//прод
                listView4.Enabled = true;//прод обобщ


                #endregion
            }
        }
        #endregion


        private void loadPlases()
        {
           
            listView1.Items.Clear(); string stk = q.tk(param.zakaz_current_13);
            dzp = q.selectAny(functionService.shorts.a("call `city`.`zakaz_plases`(", stk, ");")); //fs.shorts.m(stk); q.sh(dzp);
            ListViewItem lvi;
            foreach (DataRow r in dzp.Rows)
            {
                lvi = new ListViewItem();
                lvi.Text = functionService.shorts.a("ПМ_", r[0]);
                lvi.Tag = r[1];
                lvi.ImageIndex = 12;
                listView1.Items.Add(lvi);
            }
        }
       
        private void Add_ProdKol()
        {

            /*functionService.prm pe = new functionService.prm("PLP_treeSelect");
            string scod = functionService.nameCod.readCod(pe.tk(1)); int icod0000 = Convert.ToInt32(scod); //1- what select
           // int icod0000 = q.tc_int(param.treeView_select_14);
                int icod = icod0000 - 10000;// h.m(icod0000);
            string sql_dml_add = sql_SAED.ADD_plase(q.tk_int(param.zakaz_current_13), icod);
           // h.m(sql_dml_add);
            q.UpdateData(sql_dml_add); 
            loadPlases();*/

            functionService.prm pe = new functionService.prm("PLP_treeSelect");
            int z = q.tk_int(param.zakaz_current_13), ip = q.tk_int(param.zakaz_plase_current_15);// fs.shorts.m(fs.shorts.aa(z,ip));
            vv.currentZakaz = z;
            string scod = functionService.nameCod.readCod(pe.tk(1)); 
            int ct0 = Convert.ToInt32(scod);
            int ct = (ct0 - 10000);
            string sqlScalar = sql_SAED.readPrice(ct, 1);
            object oPrice = q.selectScalar(sqlScalar);
           
           // string sqldml = sql_SAED.ADD_prod(ct,  oPrice , q.tk_double(param.zakaz_kol_17), 0, z, ip);
            string sqldml = sql_SAED.ADD_prod(ct, oPrice, vv.kol_DBL, 0, z, ip);
            q.UpdateData(sqldml);

            refrashlistView2Prod();
            
            //string sss = MI_SUMMA_CASSA_L;
            //q.put((int)param.kassa_sum_smena_46, sss);
            //this.Text = string.Concat("Сумма по кассе :", sss);
        }
        private void refrashlistView2Prod()
        {
            int z = q.tk_int(param.zakaz_current_13), ip = q.tk_int(param.zakaz_plase_current_15);
            dztp = q.selectAny(sql_SAED.select_tov_plase(z, ip)); 
            listView2.Items.Clear();
            ListViewItem lvi;  ListViewItem.ListViewSubItem lvis;

            for (int i = 0; i < dztp.Rows.Count; i++)
            {
                lvi = new ListViewItem();
                lvi.Text = dztp.Rows[i][0].ToString();//name

                lvis = new ListViewItem.ListViewSubItem();
                lvis.Text = dztp.Rows[i][1].ToString(); //kol
                lvi.SubItems.Add(lvis);

                lvis = new ListViewItem.ListViewSubItem();
                lvis.Text = dztp.Rows[i][2].ToString();//pr
                lvi.SubItems.Add(lvis);

                lvis = new ListViewItem.ListViewSubItem();
                lvis.Text = dztp.Rows[i][3].ToString();//sd
                lvi.SubItems.Add(lvis);

                lvi.Tag = dztp.Rows[i][4].ToString(); //cod123
                lvi.ImageIndex = 2;
                this.listView2.Items.Add(lvi);
            }
            this.listView2.Columns[3].Text = functionService.shorts.a("Сумма = ", dztp.Compute("sum(sd)", "cod123>0"));
            this.listView2.Columns[1].Text = functionService.shorts.a("Кол = ", dztp.Compute("sum(kol)", "cod123>0"));

            refrashlistView4Prod();
            
        }

        private void refrashlistView4Prod()
        {
            //h.m(90);
            int z = q.tk_int(param.zakaz_current_13); //dzt.Clear(); dzt_itogo.Clear();
            dzt = q.selectAny(sql_SAED.select_zakaz(z)); //q.sh(dzt);
                  dzt_itogo = q.selectAny(sql_SAED.select_zakaz_sum(z)); //q.sh(dzt_itogo);
            listView4.Items.Clear();
            ListViewItem lvi;
            ListViewItem.ListViewSubItem lvis;
            int ipo = 0;

            for (int i = 0; i < dzt_itogo.Rows.Count; i++)
            {
                lvi = new ListViewItem();
                lvi.Text = dzt_itogo.Rows[i][0].ToString();//name

                lvis = new ListViewItem.ListViewSubItem();
                lvis.Text = dzt_itogo.Rows[i][1].ToString(); //kol
                lvi.SubItems.Add(lvis);

                lvis = new ListViewItem.ListViewSubItem();
                lvis.Text = dzt_itogo.Rows[i][2].ToString();//pr
                lvi.SubItems.Add(lvis);

                lvis = new ListViewItem.ListViewSubItem();
                lvis.Text = dzt_itogo.Rows[i][3].ToString();//sd
                lvi.SubItems.Add(lvis);

                this.listView4.Items.Add(lvi);
            }
            object sumz = dzt.Compute("sum(sd)", "cod123>0");
            this.listView4.Columns[3].Text = functionService.shorts.a("Сумма = ", sumz); 
            this.listView4.Columns[1].Text =  functionService.shorts.a("Кол = ", dzt.Compute("sum(kol)", "cod123>0"));
            //---
            q.put((int)param.zakaz_sum_45, sumz);
           
        }
        public string MI_SUMMA_CASSA_L
        {
            get
            {
                string kafe_smena_42_11214 = q.tk((int)param.kafe_smena_42);
                string sql_count = string.Concat("select count(codd) as cc from city.zakaz_rd where Left(codd,5) = ", kafe_smena_42_11214, ";");
                string sql_skalar = string.Concat("SELECT sum(kol*pr) as sumKassa FROM zakaz_td z left join zakaz_rd r on z.codd = r.codd where (r.fpa = 1 and (Left(z.codd,5) = ", kafe_smena_42_11214, ")) group by Left(z.codd,5) order by z.codd;");
                object oc = q.selectScalar(sql_count);
                object os = 0; string s = "0";
                if (Convert.ToInt32(oc) > 0) { os = q.selectScalar(sql_skalar); }
                try { s = os.ToString(); }
                catch { s = "0"; }

                return s;
            }
        }
        private void listView3_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                functionService.prm rrr = new functionService.prm("PLP_reports_chek");
                string s = listView3.SelectedItems[0].Text; object o11 = listView3.SelectedItems[0].Tag; int curIndexIm = listView3.SelectedItems[0].ImageIndex;
                q.put(param.zakaz_current_13, o11);
                rrr.put(1, o11);
                //string s900 = q.tk(param.zakaz_current_13); h.m(h.az(o11, s900));
                buttonPlaseAdd(s); buttonProdAddno(); loadPlases();
                listView2.Items.Clear();
                if (curIndexIm == 15)
                {
                    q.put((int)param.select_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz_47, 1 );
                    vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz = 1;
                    q.put((int)param.type_doc_select_49, 1);
                    setModeWAED(ETALON.ZAKAZ.modeWAED.selectZakazFiskal);
                }
                if (curIndexIm == 3)
                {
                    q.put((int)param.select_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz_47, 2);
                    vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz = 2;
                    q.put((int)param.type_doc_select_49, 2);
                   // setModeWAED(ETALON.ZAKAZ.modeWAED.selectZakazFiskal);
                    setModeWAED(ETALON.ZAKAZ.modeWAED.selectZakazPredchek );
                }
                else 
                { 
                    q.put((int)param.type_doc_select_49, 0); 
                    q.put((int)param.select_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz_47, 0);
                    vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz = 0;
                    setModeWAED(ETALON.ZAKAZ.modeWAED.selectZakaz);
                    listView2.Enabled = true; 
                }
                refrashlistView4Prod();
            }
            catch { buttonPlaseAddno(); buttonProdAddno(); }

        }
        private void buttonPlaseAdd(string stxt)
        {
            toolStripLabel1.Enabled = true; toolStripLabel1.Text = stxt; toolStripButton5.Enabled = true; toolStripButton7.Enabled = true;
        }
        private void buttonPlaseAddno()
        {
            toolStripButton5.Enabled = false; toolStripButton7.Enabled = false; toolStripLabel1.Enabled = false;
        }
        private void buttonProdAdd(string stxt)
        {
            toolStripLabel2.Enabled = true; toolStripLabel2.Text = stxt;
            toolStripButton1.Enabled = true;
            toolStripButton2.Enabled = true;
            toolStripButton6.Enabled = true;
        }
        private void buttonProdAddno()
        {
            toolStripLabel2.Enabled = false;
            toolStripButton1.Enabled = false;
            toolStripButton2.Enabled = false;
            toolStripButton6.Enabled = false;
        }

        #endregion

        private void listView2_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            selectProd();
        }
        private void selectProd()
        {
            try
            {
                string s123 = listView2.SelectedItems[0].Tag.ToString();
                q.put(param.zakaz_td_cod123_19, s123);
                object fpa_td_inCod123 = q.selectScalar(string.Concat("SELECT fpa FROM zakaz_td WHERE cod123 = ", s123));
                if (fpa_td_inCod123.ToString() == "0")
                {
                    setModeWAED(ETALON.ZAKAZ.modeWAED.deleteOrEdit_Prod);
                }
                else 
                {
                    setModeWAED(ETALON.ZAKAZ.modeWAED.addProdPredchek);
                }
            }
            catch { }
        }
        private void delProd()
        {
            int c123 = q.tk_int(param.zakaz_td_cod123_19);
            string sqldml = sql_SAED.DEL_zakaz_td_oneRow(c123); q.UpdateData(sqldml);
            calkCassa();
            refrashlistView2Prod();
        }
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            delProd(); setModeWAED(ETALON.ZAKAZ.modeWAED.deleteOrEdit_ProdAfter);
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            int icd = q.tk_int(param.zakaz_current_13);
            q.UpdateData(sql_SAED.DEL_zakaz_td(icd));
            q.UpdateData(sql_SAED.DEL_zakaz_rd(icd));
            listView3.SelectedItems[0].Remove(); clearAlllv();
        }
        private void clearAlllv()
        { 
            listView1.Items.Clear(); listView2.Items.Clear(); listView4.Items.Clear(); 
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            try
            {
                q.UpdateData(sql_SAED.DEL_plase(q.tk_int(param.zakaz_current_13), q.tk_int(param.zakaz_plase_current_15)));
                listView1.SelectedItems[0].Remove();
                calkCassa();
            }
            catch { }
            
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            editp();
        }
        private void editp() 
        {
            try 
            {
               FormKolZakaz f = new FormKolZakaz(); f.ShowDialog(); 
               vv.kol = f.valueEnter;
                q.UpdateData(sql_SAED.UPDATE_td(q.tk_int(param.zakaz_td_cod123_19), f.valueEnter));
                calkCassa();
               refrashlistView2Prod(); 
            }
            catch { }
        }
        private void listView2_DoubleClick(object sender, EventArgs e)
        {
            editp();
        }

        private void printChek_FiscalRegistrator()
        {

            object currentCheck, currentCheckSumm;
            /*			
                       fr.FROpenDLL();
                       //fr.FRStartSeans();
                       fr.FRShiftOpen("hello");
						
                       DataTable dt = new DataTable();
                       dt.Columns.Add("TovName", typeof(string));
                       dt.Columns.Add("TovS", typeof(double ));
                       DataRow row = dt.NewRow();
                       row["TovName"] = "Товар1";
                       row["TovS"] = 12000.33;
                       dt.Rows.Add(row);
			
                       row = dt.NewRow();
                       row["TovName"] = "Товар2 С супер длинным названием";
                       row["TovS"] = 1200.33;
                       dt.Rows.Add(row);
		
                       fr.FRPrintCheckFree(dt);
                       fr.FRCloseDLL();*/

            fr.OpName = vv.o_l_0_name ;//q.tk_name(param.o_l_0);
            fr.Psw = "AERF";
            fr.DevName = "COM1";
            fr.FlagOem = 1;

            fr.FROpenDLL();
            //fr.FRShiftOpen("смена 112140");
            int intCurrentZakaz = q.tk_int(param.zakaz_current_13);
            DataTable tfr = q.selectAny(REPORTS.sql.print_chek_FR(intCurrentZakaz));

           //  q.sh(tfr);
            string sspok = q.tk(param.zakaz_sum_pokup_41).Replace('.',',');
            fr.FRCheckPrint(tfr, Convert.ToDecimal(sspok ));
           // fr.FRPrintCheckFree(tfr);




           // fr.FRFreeDocCutPlus(20);
            fr.FRCloseDLL();
          //  dt = q.selectAny((iCurrentZakaz));

            //h.m("ok");
        }
        private void printChek_Free()
        {
            /*
             * type_doc_select_49 = 0 - документ в оформлении - можно печатать предчек и установить предчек
             * type_doc_select_49 = 1 - документ фискализирован - можно печатать предчек
             * type_doc_select_49 = 2 - документ пердчек - можно печатать предчек
             */
            if (q.tk((int)param.type_doc_select_49) == "0")
            {
                q.put((int)param.select_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz_47, 20);
                vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz = 20;
                setPredchekDoc();
            }
            fr.OpName = vv.o_l_0_name;//q.tk_name(param.o_l_0);
            fr.Psw = "AERF";
            fr.DevName = "COM1";
            fr.FlagOem = 1;

            fr.FROpenDLL();

            int intCurrentZakaz = q.tk_int(param.zakaz_current_13);
            DataTable tfr = q.selectAny(REPORTS.sql.print_chek_FR(intCurrentZakaz));

            fr.FRFDocPrintCheckFree(tfr);

            fr.FRCloseDLL();
        }


        private void openChek() { prnt.chek();
            printPreviewDialog1.AutoScaleMode = AutoScaleMode.None;
            //printPreviewDialog1.DesktopLocation = new Point(500, 500);
            printPreviewDialog1.Bounds = new Rectangle(0,0,350,600);
           // printPreviewDialog1.RestoreBounds = new Rectangle(300, 300, 350, 700);
            printPreviewDialog1.ShowDialog(); }
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //openChek();
            string sumChek = q.tk((int)param.zakaz_sum_45); double dSum = Convert.ToDouble(sumChek);
            FormKolZakaz k = new FormKolZakaz(dSum); k.ShowDialog(); 

            if (k.IsSelect)
            {
               // double d_valueEnter = Convert.ToDouble(k.valueEnter); double sdacha = d_valueEnter - dSum;
                q.put((int)param.zakaz_sum_pokup_41, k.valueEnter); 
                printChek_FiscalRegistrator();
                if (vv.fiskalDocAfterPrint_0no_1yes == 1) { fiskalDoc(); }
            }
        }

        private void printDocument1_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            prnt.currentPage = 0;
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            prnt.printDocument_PrintPage(e);
        }

        private void listView3_DoubleClick(object sender, EventArgs e)
        {
            openChek();
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem4_Click_1(object sender, EventArgs e)
        {
            int iw = q.tk_int(param.tree_position_Width_35);
            int iwp = 0;// iw + 50; 
            if (iw > 50) { iwp = iw - 50; } else { iwp = iw; }
            q.put(param.tree_position_Width_35, iwp);
        }

        private void toolStripMenuItem3_Click_1(object sender, EventArgs e)
        {
            //if (toolStripMenuItem3.Checked == true) { q.put(param.show_fiskal_doc_888show_0hide_39, 0); }
            //else { q.put(param.show_fiskal_doc_888show_0hide_39, 888); }

            if (vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz != 888) 
            { 
                toolStripMenuItem3.Checked = true;     vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz = 888; 
            }
            else 
            { 
                toolStripMenuItem3.Checked = false;    vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz = 0;
            }
            string stt = lableSet(vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz);
            toolStripStatusLabel1.Text = stt; toolStripMenuItem3.Text = stt;
            load();
        }
        private string lableSet(int i888i0)
        {
            string s = "Заказы находящиеся в исполнении";
            if (i888i0 == 888) { s = "Все заказы"; toolStripMenuItem3.Checked = false; }
            return s;
        }
        private void fiskalDoc()
        {
            /*q.put(param.zakaz_current_13, o11);
                          rrr.put(1, o11);*/
            if (q.tk((int)param.fiskalDocAfterPrint_0no_1yes_40) == "1")
            {
                    q.UpdateData(sql_SAED.UPDATE_rd(q.tk_int(param.zakaz_current_13), 1));
                    load();
            }
            else 
            {
                if (MessageBox.Show("Документ будет фискализирован", "Подтверждение", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    q.UpdateData(sql_SAED.UPDATE_rd(q.tk_int(param.zakaz_current_13), 1));
                    load();
                    //h.m("ok");
                }
            }
        }
        private void setPredchekDoc()
        {
            /*q.put(param.zakaz_current_13, o11);
                          rrr.put(1, o11);*/
            if (q.tk((int)param.fiskalDocAfterPrint_0no_1yes_40) == "1")
            {
                q.UpdateData(sql_SAED.UPDATE_rd(q.tk_int(param.zakaz_current_13), 2));//1-fiskal;2-predchek
                q.UpdateData(sql_SAED.UPDATE_td_fpa_WhereCodd(q.tk_int(param.zakaz_current_13),2 ));
                /*
                 * в таблице товара вносим в поле fpa значение 2, которое показывает какой товар был 
                 * отпечатан в предчеке и когда будет необходимо редактировать предчек, после внесения товара уже после
                 * печати предчека, когда часть товара в нем зафиксирована, а часть только что внесена...
                 * Для того, чтоб отделить вновь внесенный товар от ранее внесенного (перед печатью Предчека)
                 * Этот признак читается при выделении строки с товаром предчека
                 * и если он =2 - редактирование и удаление запрещается
                 * а если он =0 - редактирование и удаление разрешается 
                 */
                load();
            }
            else
            {
                if (MessageBox.Show("Будет сформирован предчек", "Подтверждение", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    q.UpdateData(sql_SAED.UPDATE_rd(q.tk_int(param.zakaz_current_13), 2));
                    load();
                    //h.m("ok");
                }
            }
        }
        private void toolStripMenuItem4_Click_2(object sender, EventArgs e)
        {
            fiskalDoc();
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {

            if (vv.fiskalDocAfterPrint_0no_1yes == 0)
            { vv.fiskalDocAfterPrint_0no_1yes = 1; toolStripMenuItem5.Checked = true; q.put(param.fiskalDocAfterPrint_0no_1yes_40,1); }
            else { vv.fiskalDocAfterPrint_0no_1yes = 0; toolStripMenuItem5.Checked = false; q.put(param.fiskalDocAfterPrint_0no_1yes_40, 1); }
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            //if (MessageBox.Show("Убрать признак фискализации", "Подтверждение", MessageBoxButtons.OKCancel) == DialogResult.OK)
            //{
            //    q.UpdateData(sql_SAED.UPDATE_rd_fpa888(0));
            //    load();
            //}

            if (MessageBox.Show("Убрать признак фискализации или предчека", "Подтверждение", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                q.UpdateData(sql_SAED.UPDATE_rd(q.tk_int(param.zakaz_current_13), 0));
                q.UpdateData(sql_SAED.UPDATE_td_fpa_WhereCodd(q.tk_int(param.zakaz_current_13), 0));
                vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz = 20;
                load();
                //h.m("ok");
            }
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            fr.OpName = vv.o_l_0_name;//q.tk_name(param.o_l_0);
            fr.Psw = "AERF";
            fr.DevName = "COM1";
            fr.FlagOem = 1;
            fr.FROpenDLL(); 
            fr.FRCancelReceipt(); fr.FRCloseDLL(); h.m("Чек АНУЛИРОВАН");
        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            //openChek();
            //string sumChek = q.tk((int)param.zakaz_sum_45); double dSum = Convert.ToDouble(sumChek);
           // FormKolZakaz k = new FormKolZakaz(dSum); k.ShowDialog();

           
                // double d_valueEnter = Convert.ToDouble(k.valueEnter); double sdacha = d_valueEnter - dSum;
              //  q.put((int)param.zakaz_sum_pokup_41, k.valueEnter);
                //printChek_FiscalRegistrator();
                printChek_Free();
               // h.m("Ок");
                //if (vv.fiskalDocAfterPrint_0no_1yes == 1) { fiskalDoc(); }
               
        }

        private void toolStripDropDownButton1_Click(object sender, EventArgs e)
        {
            calkCassa();
        }
        private void setViewDoc()
        {
            ArrayList a = new ArrayList(); 
            a.Add("Все заказы (888)");
            a.Add("Заказы с предчеком и в исполнении(20)");
            a.Add("Заказы в исполнении (0)");
            a.Add("Заказы с предчеком (2)");
            a.Add("Заказы с фискализированным чеком (1)");
            listAny l = new listAny(a); l.ShowDialog();
            q.put((int)param.select_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz_47, l.sCod);
            q.put((int)param.name_47__48 , l.sName);
            vv.iselect_888All_0vispoln_1fiskal_2predch_20predchVispol_zakaz = Convert.ToInt32(l.sCod);
            toolStripMenuItem9.Text = l.sText;
            load();
        }
        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            setViewDoc();
        }

        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            fr.OpName = vv.o_l_0_name;//q.tk_name(param.o_l_0);
            fr.Psw = "AERF";
            fr.DevName = "COM1";
            fr.FlagOem = 1;

            fr.FROpenDLL();
            fr.FRXReport();
            fr.FRCloseDLL();
        }

        private void lookDocInKA()
        {
            try
            {
                FormKolZakaz f = new FormKolZakaz("Введите номер документа С"); f.modeEnter = 1; f.ShowDialog();
                int intS = Convert.ToInt32(f.valueEnter);
                FormKolZakaz ff = new FormKolZakaz("Введите номер документа ПО"); ff.modeEnter = 1; ff.ShowDialog();
                int intP = Convert.ToInt32(ff.valueEnter);

                fr.OpName = vv.o_l_0_name;//q.tk_name(param.o_l_0);
                fr.Psw = "AERF";
                fr.DevName = "COM1";
                fr.FlagOem = 1;

                fr.FROpenDLL();
                fr.FREJNoReport(intS, intP);
                fr.FRCloseDLL();
            }
            catch { h.m("Ошибка. При указании номеров чеков введите целое число"); }
        }

        private void toolStripMenuItem11_Click(object sender, EventArgs e)
        {
            lookDocInKA();
        }
    }
    namespace REPORTS
    {
        public class PRINT
        {
            functionService.prm_city q;

            functionService.prm rr;

            REPORTS.reports rp; 

            public  int linesPrint, currentPage; DataTable dt;

            int iCurrentZakaz;

            public PRINT()  {  InitializeComponent();    }
            #region ini
            private void InitializeComponent() 
            { 
                q = new functionService.prm_city(); q.con.pass =  q.tk(param.pass_inServ_55); q.con.co(q.tk(param.server_1)); iCurrentZakaz = 0;
                
            }
            #endregion

            #region prinPages

            public void printDocument_PrintPage(System.Drawing.Printing.PrintPageEventArgs e)
            {
                if (rp == reports.check )
                {
                    chek_print(e);
                }
                else if (rp == reports.prihod)
                {
                    //printPreview_assWeek(e);
                }
            }
            #endregion

            #region allReports
            public void chek()
            {
                //назначение функции: 
                //назначить значения переменным currentPage = 0; rp = reports.ost_per;
                //забрать ДАННЫЕ С СЕРВЕРА в таблицу DataSet 
                rr = new functionService.prm("PLP_reports_chek");
                currentPage = 0; rp = REPORTS.reports.check; //h.m(rr.tk(1));
                dt = new DataTable(); iCurrentZakaz = Convert.ToInt32(rr.tk(1)); //h.m(iCurrentZakaz);
                dt = q.selectAny(sql.print_chek(iCurrentZakaz));
            }
            public void chek_print(System.Drawing.Printing.PrintPageEventArgs e)
            {
                Graphics g = e.Graphics; string oo = ""; string om = "";
                
                currentPage++;
                //$$$ Настройка страницы            
                e.PageSettings.Margins.Top = 20; e.PageSettings.Margins.Left = 20; e.PageSettings.Margins.Right = 20; e.PageSettings.Margins.Bottom = 50;

                int xr = 40, yr = 15, x = 20, y = 25 + yr;

                #region  Rectangle and Width and Heght and fornt
                Rectangle r1, r2, r3,r4;
                Rectangle rHeadline, rPageNumber;
               
                //ширина столбцов и размер шрифта
                int w1 = 140, w2 = 33, w3=33, w4 = 44;
                int h = 15, hh = 30, f = 8, ff = 10;
                
                #endregion

                #region other declaration
                int ic = dt.Rows.Count;
                string n0 = "", n5 = "", n10 = "", n15 = "";
                //colorr c0 = colorr.white, c5 = colorr.white, c10 = colorr.white, c15 = colorr.white;

                object os = dt.Compute("sum(sd)", "kol>0");
                //pr.put((int)param.percent_OST_54, String.Format("{0:0.00}", os));
                //string sz = string.Concat("Период с ",
                //    pr.take(35), " по ", pr.take(36), " Итого Средний % = ", String.Format("{0:0.00}", os));
                string schekHeader = string.Concat("Заказ №", iCurrentZakaz, "  Итого: ", os, "р.");
                #endregion

                #region Aligment
                System.Drawing.StringFormat sfc = new StringFormat(); sfc.Alignment = StringAlignment.Center;
                System.Drawing.StringFormat sfo = new StringFormat(); sfo.Alignment = StringAlignment.Far;
                System.Drawing.StringFormat sfg = new StringFormat(); sfg.LineAlignment = StringAlignment.Far;
                #endregion

                rHeadline = new Rectangle(xr, yr, 500, h + 5); rPageNumber = new Rectangle(xr + 650, yr, 70, h + 5);

                if (currentPage == 1) g.DrawString(schekHeader, new Font("Arial", 8), Brushes.Black, rHeadline);


                r1 = new Rectangle(x, y, w1, h);
                r2 = new Rectangle(x + w1, y, w2, h);
                r3 = new Rectangle(x + w1 + w2, y, w3, h);
                r4 = new Rectangle(x + w1 + w2 + w3, y, w4, h);


                #region drawStr
                g.DrawString("Наименование", new Font("Arial", f), Brushes.Black, r1);
                g.DrawString("Кол", new Font("Arial", f), Brushes.Black, r2, sfo);
                g.DrawString("Цена", new Font("Arial", f), Brushes.Black, r3, sfo);
                g.DrawString("Сумма", new Font("Arial", f), Brushes.Black, r4, sfo);
                #endregion
                y += h;
                // g.DrawString(string.Concat("Стр.", currentPage), new Font("Arial", 12), Brushes.Black, rPageNumber, sfo);

                while (linesPrint < ic)
                {
                    #region r1---r21
                    r1 = new Rectangle(x, y, w1, h);
                    r2 = new Rectangle(x + w1, y, w2, h);
                    r3 = new Rectangle(x + w1 + w2, y, w3, h);
                    r4 = new Rectangle(x + w1 + w2 + w3, y, w4, h);

                    g.DrawRectangle(Pens.LightSteelBlue, r1);
                    g.DrawRectangle(Pens.LightSteelBlue, r2);
                    g.DrawRectangle(Pens.LightSteelBlue, r3);
                    g.DrawRectangle(Pens.LightSteelBlue, r4);
                    #endregion

                    //m(string.Concat(tz.Rows[linesPrint][0], " ", tz.Rows[linesPrint][1], " ", tz.Rows[linesPrint][2]));
                    #region drawStr

                    g.DrawString(functionService.shorts.n(dt.Rows[linesPrint][0]), new Font("Arial", f), Brushes.Black, r1);
                    g.DrawString(functionService.shorts.n(dt.Rows[linesPrint][1]), new Font("Arial", f), Brushes.Black, r2, sfo);
                    g.DrawString(functionService.shorts.n(dt.Rows[linesPrint][2]), new Font("Arial", f), Brushes.Black, r3, sfo);
                    g.DrawString(functionService.shorts.n(dt.Rows[linesPrint][3]), new Font("Arial", f), Brushes.Black, r4, sfo);
                    #endregion

                    y += h; linesPrint++;
                    if (y >= e.PageBounds.Height - 50) { e.HasMorePages = true; return; }

                }
                linesPrint = 0; e.HasMorePages = false;
            }

            #endregion
           
        }
        public class sql
        {
            public static string print_chek(int coddo)
            {
                string sql_dml = string.Concat("CALL city.select_zakaz_sum_print(", coddo, ");"); return sql_dml;
            }
            public static string print_chek_FR(int coddo)
            {
                string sql_dml = string.Concat("CALL city.select_zakaz_sum_print_FR(", coddo, ");"); return sql_dml;
            }
        }
    }
    public class sql_SAED
    {
        public static string print_chek(int coddo)
        {
            string sql_dml = string.Concat("CALL select_zakaz_sum_print(", coddo, ");"); return sql_dml;
        }
        public static string UPDATE_td(int cod123o, object kol)
        {
            string sql_dml = string.Concat("update zakaz_td set kol=", kol ," where cod123=", cod123o, ";"); return sql_dml;
        }
        public static string UPDATE_td_fpa_WhereCodd(int coddo, object fpa2)
        {
            string sql_dml = string.Concat("update zakaz_td set fpa=", fpa2, " where codd=", coddo, ";"); return sql_dml;
        }
       
        public static string UPDATE_rd_fpa888(object fpa)
        {
            string sql_dml = string.Concat("update zakaz_rd set fpa=", fpa, ";"); return sql_dml;
        }
        public static string UPDATE_rd(int coddo, object fpa)
        {
            string sql_dml = string.Concat("update zakaz_rd set fpa=", fpa, " where codd=", coddo, ";"); return sql_dml;
        }
        public static string DEL_zakaz_rd(int coddo)
        {
            string sql_dml = string.Concat("delete from zakaz_rd where codd = ", coddo, ";"); return sql_dml;
        }
        public static string DEL_zakaz_td_oneRow(int cod123o)
        {
            string sql_dml = string.Concat("delete from zakaz_td where cod123 = ", cod123o, ";"); return sql_dml;
        }
        public static string DEL_zakaz_td(int coddo)
        {
            string sql_dml = string.Concat("delete from zakaz_td where codd = ", coddo, ";"); return sql_dml;
        }
        public static string DEL_plase(int coddo, int posmo)
        {
            string sql_dml = string.Concat("delete from zakaz_td where codd = ", coddo, " and posm=", posmo, ";"); return sql_dml;
        }
        public static string ADD_zakaz(int coddo, int idd, int codsotro)
        {
            string sql_dml =
            string.Concat("insert into zakaz_rd(codd, codo, codio, dd, codsotr, fpa, codkl)",
            "VALUES (", coddo, ",   0,     1, ", idd, ", ", codsotro, ",   0,     0);");
            return sql_dml;
        }
        public static string ADD_plase(int codd_zakaz_current, int codPlase, int defaultValueCT, double doublePrOut)
        {
            string sql_dml =
            string.Concat("insert into zakaz_td(ct, pr, kol, numcod, codd, posm )",
            "VALUES (", defaultValueCT, ",   ", doublePrOut.ToString().Replace(',','.'), ", 1, 1, ", codd_zakaz_current, ",  ", codPlase, ");");
            return sql_dml;
        }
        public static string readPrice(int cto, int numberColumnPrice)
        {
            string sql_dml =
            string.Concat("select pr", numberColumnPrice, " from listtov where cod=", cto, ";");
            return sql_dml;

        }
        public static string ADD_prod(int cto, object  pro, double kol, int numcodo, int codd_zakaz_current, int codPlase)
        {
            string sql_dml =
            string.Concat("insert into zakaz_td(ct, pr, kol, numcod, codd, posm )",
                          "VALUES (", cto, ", ", pro.ToString().Replace(',', '.'), ", ",
                                                 kol.ToString().Replace(',', '.'), ", ", numcodo, ", ", codd_zakaz_current, ",  ", codPlase, ");");
            return sql_dml;

        }
        public static string select_tov_plase(int codd_zakaz_current, int codPlase)
        {
            string sql =
            string.Concat("call  `city`.`select_td_plase` (", codd_zakaz_current, ", ", codPlase, ");");
            return sql;

        }
        public static string select_zakaz_sum(int codd_zakaz_current)
        {
            //`city`.`zakaz_select`(codd_10913001 int )
            string sql =
            string.Concat("call  `city`.`select_zakaz_sum` (", codd_zakaz_current, ");");
            return sql;

        }
        public static string select_zakaz(int codd_zakaz_current)
        {
            //`city`.`zakaz_select`(codd_10913001 int )
            string sql =
            string.Concat("call  `city`.`zakaz_select` (", codd_zakaz_current, ");");
            return sql;

        }
    }
}
